package com.sh.sales.happysales.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.sh.polymedicine.happysale.net.entity.UserToken;
import com.sh.sales.happysales.R;
import com.sh.sales.happysales.app.utils.LoginUtil;
import com.sh.sales.happysales.base.BaseActivity;
import com.sh.sales.happysales.datautils.LocalTokenUtils;
import com.sh.sales.happysales.service.LoginObsever;

/**
 * Created by zhouyucheng on 2017/9/18.
 */

public class WelcomActivity extends BaseActivity implements LoginObsever {
    private  String TAG= "WelcomActivity";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        UserToken mtoken = LocalTokenUtils.queryFirst();
        if (mtoken==null){
            Log.i(TAG, "onCreate: "+"nullnullnullnullnullnull");
        }else {
            Log.i(TAG, "onCreate: "+mtoken.getCode());
        }
        mAppManager.addActivity(this);
        mLoginObserable.registerObserver(this);
        //UserToken mtoken= null;
        if (null == mtoken) {
            StartActivityToLogin();
        } else {
            LoginUtil.getInstance().catchUserInfo(mtoken, WelcomActivity.this, 3);
        }
    }

    private void StartActivityToLogin() {
        Intent mInetent = new Intent();
        mInetent.setClass(WelcomActivity.this, LoginActivity.class);
        startActivity(mInetent);
        mAppManager.finishActivity(this);
    }

    @Override
    public void refreshUserSign(int intentPosition) {
        Intent mIntent = new Intent();
        switch (intentPosition) {
            case 1:
                mIntent.setClass(this, BindPhoneActivity.class);
                break;
            case 2:
                mIntent.setClass(this, PerfectInfoActivity.class);
                break;
            case 3:
                mIntent.setClass(this, ContentActivity.class);
                break;
        }
        startActivity(mIntent);
        mAppManager.finishActivity(this);
    }

    @Override
    protected void onDestroy() {
        mLoginObserable.unregisterObserver(this);
        super.onDestroy();
    }
}
